import {
    CrownCADPlugin
} from "../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";

export class PluginCommand {
    constructor() {
    }

    /**
     * 初始化，创建 SDK 插件对象
     */
    init(maxWidth = 500, minWidth = 500, maxHeight = 550, cb = () => {
    }) {
        const base_url = `${window.location.protocol}//${window.location.hostname}:8080`;
        const crownCADPlugin = new CrownCADPlugin();
        crownCADPlugin.setParentOrigin(base_url);
        crownCADPlugin.setMaxAttempt(20);
        crownCADPlugin.setLayout(maxWidth, minWidth, maxHeight);
        this.commandId = Math.floor(Math.random() * 0x1000000);
        crownCADPlugin.setCommandId(this.commandId);
        crownCADPlugin.connect().then(() => {
            this.command = crownCADPlugin.command;
            this.request = crownCADPlugin.request;
            this.query = crownCADPlugin.query;
            this.toolQuery = crownCADPlugin.toolQuery;
            this.event = crownCADPlugin.event;
            let appElement = document.getElementById('app');
            appElement.style.overflowY = 'auto';
            cb();
        });
    }

    setName(nameString, nameList) {
        let num = nameList.length + 1;
        let name;
        do {
            name = nameString + num++;
        } while (!this.validateLoadName(name, nameList));
        return name;
    }

    validateLoadName(name, nameList) {
        for (let value of nameList) {
            if (name === value) {
                return false;
            }
        }
        return true;
    }

    changePluginContainerHeight(params) {
        if (this.toolQuery) {
            this.toolQuery.changePluginContainerHeight(params);
        }
    }

    //关闭当前对话框
    closeActiveCmd() {
        if (this.toolQuery) {
            this.toolQuery.closeActiveCmd();
        }
    }

    //设置框选是否可用
    setDragSelectEnabled(enabled) {
        if (this.toolQuery) {
            this.toolQuery.setDragSelectEnabled(enabled);
        }
    }

    // tips提示接口
    eventBusAlertMsg(message, type) {
        if (this.event) {
            let msg = {type: type, description: message};
            this?.event.messageTip(msg);
        }
    }

    //清除预览数据
    clearPreview() {
        if (this.event) {
            this.event.clearPreview();
        }
    }

    // element拾取
    handlePickElement(cb) {
        this.event.subscribeElementPickEvent(res => (
            cb(res)
        ))
    }

    //设置拾取类型
    setSelectType(elementTypes) {
        if (this.event) {
            this.event.setSelectType(elementTypes);
        }
    }

    resetSelectType() {
        if (this.event) {
            this.event.resetSelectType();
        }
    }

    getEntitiesByIds(entityIds, cb = {}) {
        let entities = [];
        this.query.getEntitiesByIds(entityIds).then(res => {
            res?.data?.entities.forEach(entity => {
                entities.push(entity);
                cb(entities);
            })
        });
    }

    /**
     * 高亮元素
     * @param elementIds
     */
    highLightElements(elementIds) {
        if (this.event) {
            let idObjs = [];
            elementIds?.forEach(id => {
                idObjs.push({elementId: id});
            })
            this.event.addHeightElement(idObjs).then();
        }
    }

    /**
     * 取消高亮元素
     * @param elementId
     */
    clearHighLightElements(elementIds) {
        if (this.event) {
            let idObjs = [];
            elementIds.forEach(element => {
                idObjs.push({elementId: element});
            });
            this.event.removeHeightElement(idObjs).then();
        }
    }

    // 特征列表拾取事件注册
    handlePickFeatureNode(cb) {
        this.event.subscribeFeaturePickEvent(res => (
            cb(res)
        ))
    }

    handlePickInstance(cb) {
        this.event.subscribeInstancePickEvent(res => (
            cb(res)
        ))
    }

    handlePickEntityItem(cb) {
        this.event.subscribeEntityItemSelectedEvent(res => (
            cb(res)
        ))
    }

    //拾取文档事件注册
    handleDocumentSelectedEvent(cb) {
        if (this.event) {
            this.event.subscribeDocumentSelectedEvent(res => {
                cb(res);
            })
        }
    }

    // 高亮实例外轮廓线
    highLightEntityOutline(entityIds) {
        if (this.toolQuery) {
            this.toolQuery.highLightEntityOutline(entityIds);
        }
    }

    // 清除实例外轮廓线的高亮
    clearEntityOutline(entityId) {
        if (this.toolQuery) {
            this.toolQuery.clearEntityOutline(entityId);
        }
    }

    // 清除所有高亮
    clearAllHighLight() {
        if (this.toolQuery) {
            this.toolQuery.clearAllHighLight();
        }
    }

    //获取当前工作空间是零件模式还是装配模式
    getWorkspaceMode(params, cb) {
        if (this.toolQuery) {
            this.toolQuery.getWorkspaceMode(params).then(retValue => {
                cb(retValue.data.workspaceMode);
            })
        }
    }

    // 高亮实例对象
    highLightInstances(instanceIds) {
        if (this.toolQuery) {
            this.toolQuery.highLightInstances(instanceIds);
        }
    }

    // 取消高亮实例对象
    clearHighLightInstances(instanceIds) {
        if (this.toolQuery) {
            this.toolQuery.clearHighLightInstances(instanceIds);
        }
    }

    // 清除高亮Instance中的要素
    clearHighLightInstanceElement(paramId) {
        if (this.toolQuery) {
            this.toolQuery.clearHighLightInstanceElement(paramId);
        }
    }

    //设置右侧导航栏的显隐
    setPluginRightNavigation(status = false) {
        if (this.toolQuery) {
            this.toolQuery.setPluginRightNavigation(status);
        }
    }

    // 界面加载的开启与关闭
    openLoading(text = "") {
        if (this.toolQuery) {
            this.toolQuery.openLoading(text);
        }
    }

    closeLoading() {
        if (this.toolQuery) {
            this.toolQuery.closeLoading();
        }
    }
}